// pages/attentions/attentions.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 'atten',
    userId: wx.getStorageSync('user').id,
    followers: [],
    followings: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      active: options.i
    })
  },
  // tab切换时
  onChange(e) {
    this.setData({
      active: e.detail.name
    })
  },
  // 获取关注者
  getFollowings() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followings',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        target: this.data.userId
      },
      dataType:'其他',
      success: (res) => {
        let filterData = res.data.replace(/\"id\"\:\s\d+/g,function(str) {
          return str.replace(/\d+/,function(str2) {
            return '"' + str2 + '"'
          })
        })
        res.data = JSON.parse(filterData)
        console.log(res)
        if (res.statusCode !== 200) return wx.showToast({
          title: '获取关注列表失败',
          icon: 'none'
        })
        res.data.data.results.forEach(item => {
          item.is_follow = true
        })
        this.setData({
          followings: res.data.data.results
        })
      }
    })
  },
  // 获取粉丝
  getFollowers() {
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followers',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        target: this.data.userId
      },
      dataType:'其他',
      success: (res) => {
        let filterData = res.data.replace(/\"id\"\:\s\d+/g,function(str) {
          return str.replace(/\d+/,function(str2) {
            return '"' + str2 + '"'
          })
        })
        console.log(res)
        res.data = JSON.parse(filterData)
        if (res.statusCode !== 200) return wx.showToast({
          title: '获取关注列表失败',
          icon: 'none'
        })
        res.data.data.results.forEach(item => {
          item.is_follow = item.mutual_follow ? true : false
        })
        this.setData({
          followers: res.data.data.results
        })
      }
    })
  },
  // 新添加关注
  addFollow(e) {
    let target_id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followings',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "POST",
      data: {
        target: target_id
      },
      success: (res) => {
        // 关注失败
        if (res.statusCode !== 201) return wx.showToast({
          title: '关注失败',
          icon: "none"
        })
        // 关注成功
        let list = this.data.active == 'atten' ? this.data.followings : this.data.followers
        list.filter(item => {
          if (item.id == target_id) {
            item.is_follow = true
          }
        })
        let changeStr = this.data.active == 'atten' ? 'followings' : 'followers'
        this.setData({
          [changeStr]: list
        })
      }
    })
  },
  // 取消关注
  cancelFollow(e) {
    let target_id = e.currentTarget.dataset.id
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user/followings/' + target_id,
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      method: "DELETE",
      success: (res) => {
        // 取消关注失败
        if (res.statusCode !== 204) return wx.showToast({
          title: '关注失败',
          icon: "none"
        })
        // 取消关注成功
        let list = this.data.active == 'atten' ? this.data.followings : this.data.followers
        list.filter(item => {
          if (item.id == target_id) {
            item.is_follow = false
          }
        })
        let changeStr = this.data.active == 'atten' ? 'followings' : 'followers'
        this.setData({
          [changeStr]: list
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取粉丝和关注者
    this.getFollowings()
    this.getFollowers()
    console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})